package in.rkvsraman.gmailgadgets.simplegadget.client;

import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.user.client.Window;

public class AgentAnimator implements RepeatingCommand {

	String name;
	JsArrayString anims;
	int i;
	boolean continuePlaying;
	GmailSimpleGadget parent;
	
	boolean loaded = false;

	public AgentAnimator(String name, GmailSimpleGadget p) {
		this.name = name;
		continuePlaying = true;
		parent = p;

	}



	@Override
	public boolean execute() {

		if (ClippyJS.isLoaded().toString().equals("false"))
			return continuePlaying;
		else
			loaded = true;

		if (i == 0) {
			loaded = false;
			anims = ClippyJS.animations();
			// Window.alert(ClippyJS.isLoaded().toString() + " " + name + " " +
			// i);
			ClippyJS.moveTo(10, 40);

		}
		ClippyJS.play(anims.get(i));

		i++;

		if (i == anims.length())
			continuePlaying = false;

		if (!continuePlaying) {

			// ClippyJS.moveTo(300, 300);
			ClippyJS.stop();
			ClippyJS.hide();
			parent.loadAgentandStart();

		}
		return continuePlaying;
		// TODO Auto-generated method stub

	}

	public void stopPlaying() {
		continuePlaying = false;
	}

}
